const express = require("express")
const conn = require("./db")
const router = express.Router()

// router.get("/index", function(req, res) {
//     res.send({ uname: "li" })
// })
// conn.query('select 1', (err, resulst) => {
//         // if (err) return console.log(err.message)
//         console.log(resulst);
//     })



// 查看数据
router.get("/userlist", function(req, res) {
        conn.query("select * from user", (err, data) => {
            if (err) return console.log(err, message)
            if (data) {
                res.send({ code: 20, msg: "获取数据失败", data: data })
            } else {
                res.send({ code: 400, mag: "获取数据失败" })
            }
        })
    })
    //增加用户接口
router.post("/adduser", function(req, res) {
    const data = req.body
    const uname = data.uname
    const gerder = data.gerder;
    const addtime = +new Date()
    const user = { uname: uname, gerder: gerder, addtime: addtime };
    console.log(user);
    conn.query("insert into user set ?", user, (err, data) => {
        if (data) {
            res.send({ code: 200, msg: "插入成功" })
        } else {
            res.send({ code: 400, msg: "插入失败" })
        }
    })
})

//删除接口
router.delete("/deluser", function(req, res) {
    conn.query("delete from user where id=?", req.query.id, function(err, data) {
        if (err) return console.log(err.message);
        if (data.affectedRows == 1) {
            res.send({ code: 200, msg: "删除成功" })
        } else {
            res.send({ code: 400, msg: "删除失败" })
        }
    })
})

//修改接口
router.post("/edituser", function(req, res) {
    const data = req.body
    const uname = data.uname
    const gerder = data.gerder;
    const addtime = +new Date()
    const user = { uname: uname, gerder: gerder, addtime: addtime };
    console.log(user);
    conn.query("update user set ? where id=?", [user, req.body.id], (err, data) => {
        console.log(88888888);
        if (data) {
            res.send({ code: 200, msg: "修改成功" })
        } else {
            res.send({ code: 400, msg: "修改失败" })
        }
    })
})


module.exports = router